"""author: wenyao
   data: 2021/5/20
   project: devopscmdb
"""
# 添加额外功能
# 计时
# 日志记录
# 权限控制
# import time
# import logging
#
# format = "%(asctime)s - %(levelname)s:%(message)s"
# logging.basicConfig(level=logging.DEBUG)
# def cost(func):
#     print("i am cost")
#     def _cost(*args, **kwargs):
#         start = time.time()
#         result = func(*args,**kwargs)
#         end = time.time()
#         print(f"{func.__name__}执行花了{end-start}s")
#         return result
#     return _cost
#
# def logfunc(func):
#     print("i am logfunc")
#     def _logfunc(*args, **kwargs):
#         result = func(*args, **kwargs)
#         logging.debug(f"执行了函数{func.__name__}")
#         return result
#     return _logfunc

#@logging   # add = logging(logfunc(cost(add)))
# @logfunc   #  --> add = logfunc(cost(add))
# @cost    # ---> add = cost(add)
# def add(a,b):
#     return a+b
#
# add(1,2)

#定义一个用户列表
user = {"root":"123456","admin":"admin"}
#接收用户从键盘的输出，输入用户名和密码
#定义一个add函数，为add函数添加两个装饰器，分别提供登陆和权限的认证
#登陆认证装饰器 -- 判断用户名和密码是否正确
#权限认证装饰器 -- 判断用户是不是root，root用户才有权限执行函数

def login(func):
    def _login(*args,**kwargs):
        if username in user and user[username] == password:
            func(*args,**kwargs)
            print(f"执行了{func.__name__}")
        else:
            print("用户名或密码不正确！")
    return _login


def power(func):
    def _power(*args,**kwargs):
        if username == "root":
            func(*args,**kwargs)
            print(f"执行了{func.__name__}")
        else:
            print("权限不足")
    return _power


@login
@power
def add(a,b):
    print(f"{a+b}")

username = input("请输入用户名：")
password = input("请输入密码：")
add(1,2)